/**
 * 用户管理
 */
var pageCurr;
var form;
$(function() {
    layui.use(['util','table'], function(){
        var table = layui.table;
        form = layui.form;
        util = layui.util;

        tableIns=table.render({
            elem: '#houseinfoList',
            url:'/houseinfo/getHouseList',
            method: 'post', //默认：get请求
            cellMinWidth: 80,
            page: true,
            request: {
                pageName: 'pageNum', //页码的参数名称，默认：pageNum
                limitName: 'pageSize' //每页数据量的参数名，默认：pageSize
            },
            response:{
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 200, //成功的状态码，默认：0
                countName: 'totals', //数据总数的字段名称，默认：count
                dataName: 'list' //数据列表的字段名称，默认：data
            },
            cols: [[
                {field:'hid', title:'房源编号',align:'center',width : 90}
                ,{field:'htitle', title:'出租标题',align:'center',width : 250}
                ,{field:'hplace', title:'房屋地点',align:'center'}
                ,{field:'harea', title:'房屋面积',align:'center'}
                ,{field:'hrent', title:'房屋租金',align:'center'}
                ,{field:'hcontacts', title:'联系人',align:'center'}
                ,{field:'hcnumber', title:'联系电话',align:'center',width : 120}
                ,{field:'htime', title:'发布时间',align:'center', templet : "<div>{{layui.util.toDateString(d.htime, 'yyyy年MM月dd日')}}</div>"
                ,width : 135}
                ,{field:'hindoor', title:'房源描述',align:'center'}
                ,{field:'uname', title:'发布人',align:'center'}
                ,{field:'uid', title:'发布人编号',align:'center'}
                ,{field:'hstate', title:'房源状态',align:'center',templet: function(row) {
	                if (row.hstate == 0) {
	                    return "未上架"
	                } else if (row.hstate == 1) {
	                    return "已上架"
	                }else if (row.hstate == 2){
	                	return "已出租"
	                }
	            }
                }
                ,{fixed:'right',title:'操作',align:'center', toolbar:'#optBar',width : 150}
            ]],
            done: function(res, curr, count){
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                //console.log(res);
                //得到当前页码
                console.log(curr);
                //得到数据总量
                //console.log(count);
                pageCurr=curr;
            }
        });

        //监听工具条
        table.on('tool(houseinfoTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'del'){
                //删除
                delUser(data,data.id,data.uname);
            } else if(obj.event === 'edit'){
                //编辑
                openUser(data,"房源信息修改");
            }else if(obj.event === 'recover'){
                //恢复
                recover(data,"房源下架");
            }
        });

        //监听提交
        form.on('submit(userSubmit)', function(data){
            // TODO 校验
            formSubmit(data);
            return false;
        });
    
    form.on('submit(HouseSubmit)', function(data){
        // TODO 校验
    	HouseSubmit(data);
        return false;
    });
});
    
    
    //搜索框
    layui.use(['form','laydate'], function(){
    	 var form = layui.form ,layer = layui.layer
         ,laydate = layui.laydate;
     //日期
     laydate.render({
         elem: '#startTime'
     });
     laydate.render({
         elem: '#endTime'
     });
     //TODO 数据校验
     //监听搜索框
        form.on('submit(searchSubmit)', function(data){
            //重新加载table
            load(data);
            return false;
        });
    });
});


function HouseSubmit(obj){
    $.ajax({
        type: "POST",
        data: $("#HouseForm").serialize(),
        url: "/houseinfo/updateHouse",
        success: function (data) {
            if (data.code == 1) {
                layer.alert(data.msg,function(){
                    layer.closeAll();
                    load(obj);
                });
            } else {
                layer.alert(data.msg);
            }
        },
        error: function () {
            layer.alert("操作请求错误，请您稍后再试",function(){
                layer.closeAll();
                //加载load方法
                load(obj);//自定义
            });
        }
    });
}
//提交表单
function formSubmit(obj){
    $.ajax({
        type: "POST",
        data: $("#userForm").serialize(),
        url: "/houseinfo/lower",
        success: function (data) {
            if (data.code == 1) {
                layer.alert(data.msg,function(){
                    layer.closeAll();
                    load(obj);
                });
            } else {
                layer.alert(data.msg);
            }
        },
        error: function () {
            layer.alert("操作请求错误，请您稍后再试",function(){
                layer.closeAll();
                //加载load方法
                load(obj);//自定义
            });
        }
    });
}

//商品发货
function addUser(){
    openUser(null,"商品发货");
}

function recover(data,title){
	$("#hid").val(data.hid);
	$("#hstate").val(data.hstate);
    var pageNum = $(".layui-laypage-skip").find("input").val();
    $("#pageNum").val(pageNum);

    layer.open({
        type:1,
        title: title,
        fixed:false,
        resize :false,
        shadeClose: true,
        area: ['550px'],
        content:$('#lowerHouse'),
        end:function(){
            cleanUser();
        }
    });
}
function openUser(data,title){
	$("#htitle").val(data.htitle);
	$("#hindoor").val(data.hindoor);
	$("#hid1").val(data.hid);
	$("#hstate1").val(data.hstate);
    var pageNum = $(".layui-laypage-skip").find("input").val();
    $("#pageNum1").val(pageNum);
    layer.open({
        type:1,
        title: title,
        fixed:false,
        resize :false,
        shadeClose: true,
        area: ['550px'],
        content:$('#setHouse'),
        end:function(){
            cleanUser();
        }
    });
}

function delUser(obj,id,name) {
    var currentUser=$("#currentUser").html();
    if(null!=id){
        if(currentUser==id){
            layer.alert("对不起，您不能执行删除自己的操作！");
        }else{
            layer.confirm('您确定要删除'+name+'用户吗？', {
                btn: ['确认','返回'] //按钮
            }, function(){
                $.post("/user/updateUserStatus",{"id":id,"status":0},function(data){
                    if (data.code == 1) {
                        layer.alert(data.msg,function(){
                            layer.closeAll();
                            load(obj);
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                });
            }, function(){
                layer.closeAll();
            });
        }
    }
}
//恢复
function recoverUser(obj,id) {
    if(null!=id){
        layer.confirm('您确定要恢复吗？', {
            btn: ['确认','返回'] //按钮
        }, function(){
            $.post("/user/updateUserStatus",{"id":id,"status":1},function(data){
                if (data.code == 1) {
                    layer.alert(data.msg,function(){
                        layer.closeAll();
                        load(obj);
                    });
                } else {
                    layer.alert(data.msg);
                }
            });
        }, function(){
            layer.closeAll();
        });
    }
}

function load(obj){
    //重新加载table
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}

function cleanUser(){
    $("#username").val("");
    $("#mobile").val("");
    $("#password").val("");
    $('#roleId').html("");
}


